﻿using System.Linq.Expressions;
using System.Reflection;

namespace Mg.ExpressionParser.Parser
{
    class NewExpressionParser : BaseExpressionParser<NewExpression>
    {
        protected override SqlPack Update(NewExpression expression, SqlPack sqlPack)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                MemberInfo m = expression.Members[i];
                ConstantExpression c = expression.Arguments[i] as ConstantExpression;
                sqlPack += m.Name + " =";
                sqlPack.AddDbParameter(c.Value);
                sqlPack += ",";
            }
            if (sqlPack[sqlPack.Length - 1] == ',')
            {
                sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
            }
            return sqlPack;
        }

        protected override SqlPack Select(NewExpression expression, SqlPack sqlPack)
        {
            foreach (Expression item in expression.Arguments)
            {
                ExpressionParserProvider.Select(item, sqlPack);
            }
            return sqlPack;
        }

        protected override SqlPack GroupBy(NewExpression expression, SqlPack sqlPack)
        {
            foreach (Expression item in expression.Arguments)
            {
                ExpressionParserProvider.GroupBy(item, sqlPack);
            }
            return sqlPack;
        }

        protected override SqlPack OrderBy(NewExpression expression, SqlPack sqlPack)
        {
            foreach (Expression item in expression.Arguments)
            {
                ExpressionParserProvider.OrderBy(item, sqlPack);
            }
            return sqlPack;
        }
    }
}
